import CustomValue2D from "./CustomValue2D";
import { MyMeshQuadTexture } from "./MyMeshQuadTexture";

export class MyShaderSprite extends Laya.Sprite {
    // 自定义mesh名称,子类需要修改
    protected meshName: string = "_myMesh";
    protected shaderValue: CustomValue2D = null;
    protected _renderTexture: Laya.Texture = null;

    public w = 100;
    public h = 100;

    attributes = [
        ["posuv", Laya.glTFAccessorComponentType.FLOAT, 4],
    ];

    constructor(shaderValue: CustomValue2D) {
        super();
        this.shaderValue = shaderValue;
        shaderValue.setAttribLocation(this.attributes);
        this.customRenderEnable = true;
    }

    public set renderTexture(texture) {
        this._renderTexture = texture;
    }

    public get renderTexture() {
        return this._renderTexture;
    }

    public customRender(context: Laya.Context, x: number, y: number): void {
        // 原本这里直接调用了drawTarget
        // 现在改成抄一边drawTarget里的代码，然后魔改
        context['_drawCount']++;
        // 自定义的mesh
        let mesh = context[this.meshName];
        if (!mesh || mesh.vertNum + 4 > Laya.Context['_MAXVERTNUM'] || mesh.released) {

            mesh = MyMeshQuadTexture.getAMesh(this.meshName, this.attributes, context.isMain);
            context.meshlist.push(mesh);
            context[this.meshName] = mesh;
        }

        this.addQuad(mesh, x, y);

        // 判断shaervalue是否一致，这里换了submit后，就进入下一个drawcall了
        if (context['_curSubmit'].shaderValue != this.shaderValue) {
            var submit = context['_curSubmit'] = Laya.SubmitTarget.create(context, mesh, this.shaderValue, this.renderTexture as any);
            submit.blendType = context['_nBlendType'];
            context['_copyClipInfo'](submit, context['_globalClipMatrix']);
            submit['_numEle'] = 6;
            mesh.indexNum += 6;
            mesh.vertNum += 4;
            context['_submits'][context['_submits']._length++] = submit;
            context['_cubSubmit'] = Laya.SubmitBase.RENDERBASE;
        } else {
            context['_curSubmit']._numEle += 6;
            mesh.indexNum += 6;
            mesh.vertNum += 4;
        }
    }

    addQuad(mesh, x, y) {
        var vb = mesh._vb;
        var vpos = (vb._byteLength >> 2);
        vb.setByteLength((vpos + mesh.const_stride) << 2);
        var vbdata = vb._floatArray32 || vb.getFloat32Array();
        var cpos = vpos;

        let vertices = this.getVertices(x, y);
        for (let i = 0; i < vertices.length; i++) {
            vbdata[cpos++] = vertices[i];
        }
        vb._upload = true;
    }

    // 在子类中覆盖该函数
    getVertices(x, y) {
        let uv = this.renderTexture['_uv'];
        let vertices = [];
        vertices.push(x);
        vertices.push(y);
        vertices.push(uv[0]);
        vertices.push(uv[1]);

        vertices.push(x + this.w);
        vertices.push(y);
        vertices.push(uv[2]);
        vertices.push(uv[3]);

        vertices.push(x + this.w);
        vertices.push(y + this.h);
        vertices.push(uv[4]);
        vertices.push(uv[5]);

        vertices.push(x);
        vertices.push(y + this.h);
        vertices.push(uv[6]);
        vertices.push(uv[7]);

        return vertices;
    }
}